﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using TFXK.BLL;
using TFXK.Common;
using log4net;
using TFXK.Model;
using System.Data;

public partial class PubTravelDocInfo : System.Web.UI.Page
{
    private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private static readonly NewsBLL bllNews = new NewsBLL();
    private static readonly CategoryBLL bllCategory = new CategoryBLL();
    private static readonly PicturesBLL bllPictures = new PicturesBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                try
                {
                    if (string.IsNullOrEmpty(Session["UserLogin"] + ""))
                    {

                        Msg.ShowAndRedirect("未登录或登录已超时!", "TravelDoc.aspx");
                    }
                }
                catch
                {
                    Msg.ShowAndRedirect("未登录或登录已超时!", "TravelDoc.aspx");
                }

                if (Request.QueryString["Key"] != null)
                {
                    int id = Int32.Parse(Request.QueryString["Key"].ToString());
                    InitData(id);
                }
                else
                {

                }
            }
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
            Response.Redirect("PubTravelDoc.aspx", false);
        }
    }
    #endregion

    #region 绑定
    private void InitData(int id)
    {
        TFXK.Model.News model = bllNews.GetModel(id);
        this.Title += "-" + model.title;
        this.txtTitle.Text = model.title;
        this.txtContent.Value = model.description;
        hdfPid.Value = model.id + "";
        hdfTid.Value = model.parentId + "";

        BindImages(model.id, model.parentId);
    }
    #endregion

    #region 绑定
    private void SaveData()
    {
        if (string.IsNullOrEmpty(TFXK.Common.StringUtil.HtmlEncode(txtTitle.Text)) || string.IsNullOrEmpty(TFXK.Common.StringUtil.HtmlEncode(txtContent.Value)))
        {
            Msg.Show("不能为空！");
            return;
        }
        else
        {
            if (string.IsNullOrEmpty(hdfPid.Value))
            {
                Customs cmodel = (Customs)Session["UserLogin"];
                TFXK.Model.News model = new TFXK.Model.News();
                model.title = HtmlEncode(txtTitle.Text);
                model.description = HtmlEncode(txtContent.Value);

                model.isOutlLink = cmodel.id + "";
                model.isSlideOn = "0";
                model.orderId = 0;
                model.parentId = 40;
                model.publisher = cmodel.loginName;
                model.source = "";
                model.state = "1";
                model.clicks = 0;
                model.createTime = DateTime.Now.ToString();
                int row = bllNews.Add(model);
                if (row > 0)
                {
                    hdfTid.Value = 40 + "";
                    hdfPid.Value = "" + row;
                    UpLoadImages();
                    Msg.ShowAndRedirect("发布成功！", "PubTravelDoc.aspx");
                }
                else
                {
                    Msg.Show("发布失败!");
                }
            }
            else
            {
                TFXK.Model.News NModel = bllNews.GetModel(int.Parse(hdfPid.Value));
                NModel.title = HtmlEncode(txtTitle.Text);
                NModel.description = HtmlEncode(txtContent.Value);
                UpLoadImages();
                bllNews.Update(NModel);
                Msg.ShowAndRedirect("修改成功！", "PubTravelDoc.aspx");
            }
        }
    }
    #endregion


    #region 图片上传
    private void UpLoadImages()
    {
        try
        {
            string saveDir = "uploads/"; //文件保存文件夹
            string serverFile = Server.MapPath(saveDir);
            int Nid = 0;
            foreach (DevExpress.Web.ASPxUploadControl.UploadedFile file in uploadControl.UploadedFiles)
            {
             
                if (!string.IsNullOrEmpty(file.FileName))
                {   Nid++;
                    string imgExt = file.FileName.Substring(file.FileName.IndexOf('.'));
                    string saveName = DateTime.Now.ToString("yyyyMMddhhmmss" + Nid) + imgExt;

                    file.SaveAs(serverFile + saveName);
                    TFXK.Model.Pictures pmodel = new TFXK.Model.Pictures();
                    pmodel.imgPath = saveName;
                    pmodel.imgAlt = file.FileName.Substring(0, file.FileName.IndexOf('.'));
                    pmodel.parentID = int.Parse(hdfPid.Value);
                    pmodel.typeID = int.Parse(hdfTid.Value);
                    bllPictures.Delete(pmodel.parentID, pmodel.typeID);
                    bllPictures.Add(pmodel);
                }
            }
        }
        catch (Exception ex)
        {
            Msg.Show("网络错误:" + ex.Message);
        }
    }
    #endregion

    #region 图片加载
    private void BindImages(int pid, int tid)
    {
        try
        {
            rptImages.DataSource = bllPictures.GetList(pid, tid);
            rptImages.DataBind();
        }
        catch (Exception ex)
        {
            Msg.Show("网络错误:" + ex.Message);
        }
    }
    #endregion


    protected void btnSave_Click(object sender, EventArgs e)
    {
        SaveData();
    }

    /// <summary>
    /// 说明:过滤危险字符
    /// 返回值：处理后的这符串。
    /// 参数：	str原字符串。
    /// 创建日期：2006-02-22
    /// 创建人：张耀庭
    /// </summary>
    public string HtmlEncode(string str)
    {
        str = str.Replace("select", "");
        str = str.Replace("insert", "");
        str = str.Replace("update", "");
        str = str.Replace("delete", "");
        str = str.Replace("create", "");
        str = str.Replace("drop", "");
        str = str.Replace("delcare", "");
        if (str.Trim().ToString() == "") { str = ""; }
        return str.Trim();
    }
}
